
//
//  SettingVController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/8.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "SettingVController.h"
#import "SetOneCell.h"
#import "SetTwoCell.h"
#import "LoginExperssController.h"
#import "AboutUSController.h"



@interface SettingVController ()<UITableViewDelegate, UITableViewDataSource>


@end

@implementation SettingVController
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureNaBar];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = kSpNone;
    [_tableView registerNib:[UINib nibWithNibName:@"SetOneCell" bundle:nil] forCellReuseIdentifier:@"SetOneCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"SetTwoCell" bundle:nil] forCellReuseIdentifier:@"SetTwoCell"];
    
}
- (void)configureNaBar {
    self.navigationItem.title = @"设置";
    //设置导航栏按钮的边距
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame= CGRectMake(0, 0, 60, 30);
    [rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
    [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    /**
     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacerleft.width = -20;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
    
    
    
}
- (void)handleBackAxtion {
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else {
        return 2;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section== 0) {
        return 83;
    }
    else {
        return 60;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    bg.backgroundColor = KShouYeBg;
    return bg;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *titA = @[@"检查更新",@"关于我们"];
    if (indexPath.section == 0) {
        SetOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SetOneCell"];
        cell.selectionStyle = kSeNone;
        if ([_isNotice isEqualToString:@"0"]) {
            cell.btn.selected = NO;
        }else {
            cell.btn.selected = YES;
        }
        [cell.btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        return cell;
    }
    else {
        SetTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SetTwoCell"];
        cell.selectionStyle = kSeNone;
        cell.titleLB.text = titA[indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            AboutUSController *ab = [[AboutUSController alloc] init];
            [self.navigationController pushViewController:ab animated:YES];
        }
    }
}

#pragma mark   －＝－＝－＝－＝－＝通知的打开与关闭－＝－＝
- (void)handleBtnAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    [HttpRequest postWithURLString:POST(Shop_PersonalEditIsNotice) parameters:parameDic success:^(id responseObject) {
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"设置通知出错"];
            return ;
        }
        NSDictionary *tempD = responseObject[@"data"];
        
        if (![[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"0"]) {
            [MBProgressHUD showText:tempD[@"msg"]];
            return;
        }

        
    } failure:^(NSError *error) {
        
    }];
    
    
}
//退出登录 (商户端)
- (IBAction)exitLoginAction:(UIButton *)sender {
    
    [SaveDataMethord cleanloginData];

    LoginExperssController *login = [[LoginExperssController alloc] init];
    [self.navigationController pushViewController:login animated:YES];
}

@end
